package auth

import (
	"common/config"
	"common/jwts"
	"common/logs"
	"net/http"

	"github.com/gin-gonic/gin"
)

func DeToken() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		token := ctx.Request.Header.Get("Token")
		uid, err := jwts.ParseToken(token, config.Conf.Jwt.Secret)
		if err != nil {
			ctx.JSON(http.StatusUnauthorized, gin.H{
				"error": "token为空",
			})
			// 终止后续处理
			ctx.Abort()
		}
		if ctx.Keys == nil {
			ctx.Keys = map[string]interface{}{}
		}
		//uid放到请求参数vs
		logs.Info("uid=%s", uid)
		ctx.Keys["uid"] = uid
		ctx.Next()
	}
}
